const app = getApp();
const imgBaseUrl = app.globalData.imgBaseUrl;
Page({
  data: {
    imgBaseUrl: imgBaseUrl,
    activeIndex:0,
    vipList:[
      {
        id:1,
        name:'月会员'
      }, {
        id: 2,
        name: '季会员'
      }, {
        id: 3,
        name: '年会员'
      },
    ]
  },
  onLoad: function (options) {
    var that=this;
    app.func.postReq('/text/index.php/home/user/buyuser',{},function(res){
      res.usertype[0].title="升级一个月会员";
      res.usertype[0].discount = res.usertype[0].discount*100;
      res.usertype[1].title="升级三个月会员";
      res.usertype[1].discount = res.usertype[1].discount*100;
      res.usertype[2].title="升级一年会员";
      res.usertype[2].discount = res.usertype[2].discount*100;
      that.setData({
        vipList:res.usertype,
        selectPrice: res.usertype[0].price,
        selectId: res.usertype[0].id
      })
    })
  },
  selectVip:function(e){
    var {index,id,price}=e.currentTarget.dataset;
    this.setData({
      activeIndex:index,
      selectId:id,
      selectPrice:price
    })
  },
  bindPhone:function (e) {
    this.setData({
      phone:e.detail.value
    })
  },
  bindVipPay:function(){
    var that=this;
    var selectId=that.data.selectId;
    var selectPrice = that.data.selectPrice;
    var phone = this.data.phone;
    if(!phone){
      wx.showToast({
        title: '手机号码必须填写！',
        icon: 'none'
      })
      return;
    }
    if(!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '手机号码格式有误！',
        icon: 'none'
      })
      return;
    }
    app.func.postReq('/text/index.php/home/user/ktuser', { grade: selectId, price: selectPrice, cphone: phone},function(res){
      wx.requestPayment({
        'timeStamp': res.timeStamp,
        'nonceStr': res.nonceStr,
        'package': res.package,
        'signType': 'MD5',
        'paySign': res.paySign,
        'success': function (res) {
          console.log(res, '支付成功');
          wx.showToast({
            title: '支付成功',
            icon: 'success'
          })
          setTimeout(function () {
            wx.navigateBack({
              
            })
          }, 1500)
        },
        'fail': function (res) {
          console.log(res, '支付失败');
          wx.showToast({
            title: '支付取消',
            icon: 'none'
          })
        }
      })
    })
  }
})